﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace DreamCMS.WebCore.Common
{
    /// <summary>
    /// Web站点开发中字符串操作辅助类（静态方法）
    /// </summary>
    public class StringHelper
    {
        /// <summary>
        /// 对字符串进行URL编码
        /// </summary>
        /// <param name="str">待编码的字符串</param>
        /// <returns></returns>
        public static string UrlEncode(string str)
        {
            return HttpUtility.UrlEncode(str);
        }

        /// <summary>
        /// 对URL字符串进行解码（URL上带的参数会被进行编码以把某些特殊符号和汉字编码为某种字符进行传输，接受到这样的字符需要进行解码才能得到真实值）
        /// </summary>
        /// <param name="encodedUrl">待解码的URL字符串</param>
        /// <returns>解码后的字符串</returns>
        public static string UrlDecode(string encodedUrl)
        {
            return HttpUtility.UrlDecode(encodedUrl);
        }

        /// <summary>
        /// 字符串转换为32位无符号整数
        /// </summary>
        /// <param name="str">待转换的字符串</param>
        /// <param name="defaultValue">转换失败后的默认值</param>
        /// <returns>转换后的整型值</returns>
        public static int StrToUInt32(string str, int defaultValue)
        {
            if(IsUnsignedNum(str))
            {
                defaultValue = int.Parse(str);
            }
            return defaultValue;
        }
        /// <summary>
        /// 字符串转换为64位无符号整数
        /// </summary>
        /// <param name="str">待转换的字符串</param>
        /// <param name="defaultValue">转换失败后的默认值</param>
        /// <returns>转换后的整型值</returns>
        public static ulong StrToUInt64(string str, uint defaultValue)
        {
            if (IsUnsignedNum(str))
            {
                defaultValue = uint.Parse(str);
            }
            return defaultValue;
        }
        /// <summary>
        /// 字符串转换为有符号的32位整数
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int StrToInt32(string str,int defaultValue)
        {
            if(IsNum(str))
            {
                defaultValue = int.Parse(str);
            }
            return defaultValue;
        }
        /// <summary>
        /// 字符串转换为带符号的64位整数
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long StrToInt64(string str, long defaultValue)
        {
            if (IsNum(str))
            {
                defaultValue = long.Parse(str);
            }
            return defaultValue;
        }

        /// <summary>
        /// 判断字符串是否是无符号整数。（不区分多少位整数）
        /// </summary>
        /// <param name="str">待判断的字符串</param>
        /// <returns>true则表示字符串是数字</returns>
        public static bool IsUnsignedNum(string str)
        {
            if(string.IsNullOrEmpty(str))
            {
                return false;
            }
            Regex rex = new Regex(@"^(0|[1-9]\d*)$");  //这个正则的意思是，要么只有一个0，要么是1-9开头再跟着多个数字。所以是判断正整数和0
            return rex.IsMatch(str);  //能匹配则表示str是数字
           
        }
        /// <summary>
        /// 判断一个字符串是否是有符号整数（正数或负数）或0.（不区分多少位）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNum(string str)
        {
            if(string.IsNullOrEmpty(str))
            {
                return false;
            }
            Regex rex=new Regex(@"^(0|[1-9]\d*|[\-][1-9]{1}\d*)$");
            return rex.IsMatch(str);
        }
        /// <summary>
        /// Object类型转为32位有符号整数Int类型
        /// </summary>
        /// <param name="obj">Object对象</param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static int ObjectToInt32(object obj,int defaultValue)
        {
            if(obj!=null)
            {
                return StrToInt32(obj.ToString(), defaultValue);
            }
            return defaultValue;
        }
        /// <summary>
        /// Object类型转换为有符号整数64位
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long ObjectToInt64(object obj, long defaultValue)
        {
            if (obj != null)
            {
                return StrToInt64(obj.ToString(), defaultValue);
            }
            return defaultValue;
        }
        /// <summary>
        /// 从头开始截取一定长度的字符串，并在结尾加上结尾字符
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="length">长度</param>
        /// <param name="endstr">结尾字符串</param>
        /// <returns>返回截取后的字符串</returns>
        public static string CutString(string str,int length,string endstr)
        {
            if(str.Length<length)
            {
                return str;
            }
            string rel = null;
            rel = str.Substring(0, length)+endstr;
            return rel;
        }
        /// <summary>
        /// 匹配并移除字符串中的HTML标签
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string RemoveHtml(string str)
        {
            string regex = @"<[^>]*>";
            string rel=Regex.Replace(str, regex, string.Empty, RegexOptions.IgnoreCase);
            regex = @"[\s*\t\n\r]";
            rel = Regex.Replace(rel, regex, string.Empty, RegexOptions.IgnoreCase);
            return rel;
        }
        /// <summary>
        /// 匹配字符串是否是http协议的网址（就是检测字符串开头是否含有Http://字样）
        /// </summary>
        /// <param name="str">源字符串</param>
        /// <returns>是否是http字符串</returns>
        public static bool IsHttpUrl(string str)
        {
            string regexstr="^http://\\w*$";
            return Regex.IsMatch(str,regexstr);
        }
        /// <summary>
        /// Html解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string HtmlDecode(string str)
        {
            return HttpUtility.HtmlDecode(str);
        }
        /// <summary>
        /// HTML编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string HtmlEncode(string str)
        {
            return HttpUtility.HtmlEncode(str);
        }
    }
}
